import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:wanchong/widget/qr_anmi.dart';

class QrScan extends StatefulWidget {
  @override
  _QrScanState createState() => _QrScanState();
}

const flash_on = "FLASH ON";
const flash_off = "FLASH OFF";

class _QrScanState extends State<QrScan> {
  QRViewController controller;
  var flashState = flash_off;
  @override
  void initState() {
    // _init();
    super.initState();
  }
  var qrText = "";
  final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Container(
          child: QRView(
            // key: qrKey,
            onQRViewCreated: _onQRViewCreated,
          ),
        ),
        Container(
          child: Column(
            children: <Widget>[
              //上半部分
              Container(
                height: ScreenUtil.getInstance().setHeight(216.0),
                color: Colors.black.withOpacity(0.3),
              ),

              Row(
                children: <Widget>[
                  //中间左半部分
                  Container(
                    height: ScreenUtil.getInstance().setHeight(258.0),
                    width: (MediaQuery.of(context).size.width - 258) / 2,
                    color: Colors.black.withOpacity(0.3),
                  ),
                  //中间扫描
                  Expanded(
                    child: QrAnmi(
                      height: ScreenUtil.getInstance().setHeight(258.0),
                      width: double.infinity,
                    ),
                  ),
                  //中间右半部分
                  Container(
                    height: ScreenUtil.getInstance().setHeight(258.0),
                    width: (MediaQuery.of(context).size.width - 258) / 2,
                    color: Colors.black.withOpacity(0.3),
                  ),
                ],
              ),
              Expanded(
                child: Container(
                  color: Colors.black.withOpacity(0.3),
                ),
              )
            ],
          ),
        ),
        // Positioned(
        //   // left: 0,
        //   // bottom: ScreenUtil.getInstance().setHeight(150)+1,
        //   child:
        // )
        Container(
          child: Column(
            children: <Widget>[
              SizedBox(
                height: ScreenUtil.getInstance().setHeight(50),
              ),
              Expanded(
                child: Container(),
              ),
              Container(
                width: double.infinity,
                height: ScreenUtil.getInstance().setHeight(120),
                color: Colors.black,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    _menu("手动输入序列号", "", () {}),
                    _menu(flashState==flash_on?"关闭手电筒":"打开手电筒", "", () {
                      if (controller != null) {
                        controller.flipFlash();
                        if (_isFlashOn(flashState))
                          setState(() {
                            flashState = flash_off;
                          });
                        else
                          setState(() {
                            flashState = flash_on;
                          });
                      }
                    }),
                  ],
                ),
              ),
              Container(
                height: ScreenUtil.getInstance().setHeight(30),
                color: Colors.black,
              )
            ],
          ),
        ),
        Positioned(
            left: ScreenUtil.getInstance().setWidth(10.0),
            top: ScreenUtil.getInstance().setWidth(50.0),
            child: GestureDetector(
              onTap: () {
                Navigator.of(context).pop();
              },
              child: Container(
                width: ScreenUtil.getInstance().setWidth(30.0),
                height: ScreenUtil.getInstance().setHeight(30.0),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(
                    ScreenUtil.getInstance().setHeight(15.0),
                  ),
                  color: Colors.black,
                ),
                child: Icon(
                  Icons.chevron_left,
                  color: Colors.white,
                ),
              ),
            ))
      ],
    );
  }

  void _onQRViewCreated(QRViewController controller) {
    final channel = controller.channel;
    controller.init(qrKey);
    this.controller = controller;
    channel.setMethodCallHandler((MethodCall call) async {
      switch (call.method) {
        case "onRecognizeQR":
          dynamic arguments = call.arguments;
          setState(() {
            qrText = arguments.toString();
          });
      }
    });
  }

  _isFlashOn(String current) {
    return flash_on == current;
  }

  Widget _menu(String title, String image, callback) {
    return GestureDetector(
      onTap: () {
        callback();
      },
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
            width: ScreenUtil.getInstance().setWidth(70.0),
            height: ScreenUtil.getInstance().setHeight(70.0),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(
                  ScreenUtil.getInstance().setWidth(35.0),
                ),
                color: Colors.white),
            child: Icon(Icons.colorize),
          ),
          SizedBox(
            height: ScreenUtil.getInstance().setHeight(8.0),
          ),
          Text(
            title,
            style: TextStyle(
                color: Colors.white,
                fontSize: ScreenUtil.getInstance().setSp(12.0),
                decoration: TextDecoration.none),
          ),
        ],
      ),
    );
  }
}
